package com.xxg.renrenorder.manager;

import com.github.wxpay.sdk.WXPayConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 微信支付配置
 *
 * @author xxg
 * @date 2020/11/19
 */
@Component
public class LpMyConfig extends WXPayConfig {
    @Value(value = "${wechat.lpAppid}")
    private String appid;
    @Value(value = "${wechat.mch_id}")
    private String mchId;
    @Value(value = "${wechat.key}")
    private String key;
    @Value(value = "${wechat.ip}")
    private String ip;
    @Value(value = "${wechat.cert_path}")
    private String certPath;
    @Value(value = "${wechat.lpnotifyurl}")
    private String notifyUrl;
    @Value(value = "${wechat.refund.lpnotifyurl}")
    private String refundNotifyUrl;

    private byte[] certData;

    public void readCert() throws Exception {
        File file = ResourceUtils.getFile(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    public String getIp() {
        return ip;
    }

    @Override
    public String getAppID() {
        return appid;
    }

    @Override
    public String getMchID() {
        return mchId;
    }

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public String getNotifyUrl() {
        return notifyUrl;
    }

    public String getReFundNotifyUrl() {
        return refundNotifyUrl;
    }

    @Override
    public boolean useSandbox() {
        return false;
    }

    @Override
    public InputStream getCertStream() {
        return new ByteArrayInputStream(this.certData);
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }
}
